
** create county characteristics file 
if 1 {
	* Module: County lvl demographics
	* 	Census 2010: fraction urban population
	* 	5y ACS 2007-2011, 2012-2016 : fraction below FPL, fraction African American, median home value 
			
	* Fraction urban population
	* 	Census 2010	
		insheet using "$Rep_smokelabor/1_build/nhgis/raw/nhgis0055_ds172_2010_county.csv", clear
		bys statea countya: assert _N==1
		
		gen cen2010_fracurb = h7w002/h7w001
		gen countyfip = statea*1000+countya
		rename statea statefip
		
		keep countyfip statefip cen2010_fracurb
		order countyfip statefip cen2010_fracurb
		
		label var countyfip "FIPs county"
		label var statefip "FIPs state"
		label var cen2010_fracurb "Census2010: Fraction of urban population"
		
		compress
		saveold "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_cen2010_fracurb.dta", replace
	
	
	* Fraction below FPL, fraction African American, median home value
	* 	ACS 2007-2011 5-yr estimates
		insheet using "$Rep_smokelabor/1_build/nhgis/raw/nhgis0055_ds184_20115_2011_county.csv", clear
		bys statea countya: assert _N==1
		
		gen acs0711_pov100 = (mpve002+mpve003)/mpve001
		gen acs0711_p50homeval = mu2m001
		gen acs0711_fracblack = mnum003/(mnum002+mnum003+mnum004+mnum005+mnum006+mnum007+mnum008)
		* 	Note: total population "mnum001" doesn't appear to be right. Use summation of all races as denominator instead.
		
		gen countyfip = statea*1000+countya
		rename statea statefip
		
		keep countyfip statefip acs0711*
		order countyfip statefip acs0711*
		
		compress
		saveold "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_acs0711_characs.dta", replace
	
	* Fraction below FPL, fraction African American, median home value
	* 	ACS 2012-2016 5-yr estimates
		insheet using "$Rep_smokelabor/1_build/nhgis/raw/nhgis0055_ds225_20165_2016_county.csv", clear
		bys statea countya: assert _N==1
		
		gen acs1216_pov100 = (af43e002+af43e003)/af43e001
		gen acs1216_p50homeval = af9le001
		gen acs1216_fracblack = af2mm003/(af2mm002+af2mm003+af2mm004+af2mm005+af2mm006+af2mm007+af2mm008)
		* 	Note: total population "mnum001" doesn't appear to be right. Use summation of all races as denominator instead.
		
		gen countyfip = statea*1000+countya
		rename statea statefip
		
		keep countyfip statefip acs1216*
		order countyfip statefip acs1216*
		
		compress
		saveold "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_acs1216_characs.dta", replace
	
	
	* Merge files
	use "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_acs0711_characs.dta", clear
	merge 1:1 countyfip using "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_acs1216_characs.dta", nogen
	merge 1:1 countyfip using "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_cen2010_fracurb.dta", nogen
	
	* Take means of acs across two non-overlapping 5-yr estimates
	foreach param in pov100 p50homeval fracblack {
		egen acs0716_`param' = rowmean(acs0711_`param' acs1216_`param')
	}
	
	drop acs0711* acs1216*
	
	label var countyfip "FIPs county"
	label var statefip "FIPs state"
	lab var acs0716_pov100 "ACS0711,1216: Fraction of pop below 100% FPL"
	lab var acs0716_p50homeval "ACS0711,1216: Median home value ($)"
	lab var acs0716_fracblack "ACS0711,1216: Fraction of pop w/ race = black alone"
	
	compress
	saveold "$Rep_smokelabor/1_build/nhgis/proc/nhgis_county_Xs.dta", replace
	
	* Clean up
	erase "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_acs0711_characs.dta"
	erase "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_acs1216_characs.dta"
	erase "$Rep_smokelabor/1_build/nhgis/proc/tmpfile_cen2010_fracurb.dta"
		
	
	
	
	
}